//
//  CustomerNetworking.m
//  AFNetworking二次封装
//
//  Created by dllo on 15/11/10.
//  Copyright (c) 2015年 杜怡方. All rights reserved.
//

#import "CustomerNetworking.h"
#import "AFNetworking.h"
#import "SCCatWaitingHUD.h"
#import "SVProgressHUD.h"

@implementation CustomerNetworking

+ (void)customerConnectWithURL:(NSString *)urlString andparameters:(NSDictionary *)parameters andBlock:(ConnectionBlock)block
{
    CustomerNetworking *customer = [[CustomerNetworking alloc] init];
    [customer connectWithURL:urlString andparameters:parameters andBlock:block];
    [customer release];
}

- (void)connectWithURL:(NSString *)urlString andparameters:(NSDictionary *)parameters andBlock:(ConnectionBlock)block
{
    //开始网络请求
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    //通过判断parameter是是否为空, 确定使用POST or GET
    //如果parameters为空, 即为GET请求, 如果不为空, 则为POST请求
    if (parameters == nil) {
        //GET请求
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/plain", @"text/html", nil];
        manager.securityPolicy.allowInvalidCertificates = YES;
        [manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            block(responseObject);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"%@", error);
            [[SCCatWaitingHUD sharedInstance] stop];
            [SVProgressHUD showErrorWithStatus:@"网络错误!"];
        }];
    }else
    {
        [manager POST:urlString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
            block(responseObject);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"%@", error);
            [[SCCatWaitingHUD sharedInstance] stop];
            [SVProgressHUD showErrorWithStatus:@"网络错误!"];
        }];
    }

    
}



@end
